package com.orkasgb.framework.springsecurity.sms;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  用于检查用户名和密码是否正确的服务器。实现自己的UserDetailService接口，并实现这个接口的方法。主要是自定义方法来返回用户信息。
 *
 * @date
 * @since
 */
@Component("orkasgbSMSUserDetailsService")
public class OrkasgbSMSUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String phoneNumber) throws UsernameNotFoundException {

        // 模拟从其他介质中查询验证码
        String code = this.getCodeByPhoneNumber(phoneNumber);// throw exception if not found
        List<SimpleGrantedAuthority> grantedAuthorities = List.of(new SimpleGrantedAuthority("ADMIN2"));
        // 构造userDetails
        return new OrkasgbSMSUserDetails(phoneNumber, code, grantedAuthorities);
    }

    private String getCodeByPhoneNumber(String phoneNumber) {
        return "239802";
    }
}
